﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GIMachineGenerator
{
    public class GIMachineWordState
    {
        private string _pattern = "";
        private string _name = "";
        private bool _isLiteral, _isCaseSensitive = false, _isAcceptable;
        private int _iStateID=0;
        private List<GIMachineWordState> _nextStates = new List<GIMachineWordState>();
        
        


        public string Pattern
        {
            get
            {
                return _pattern;
            }
            set
            {
                _pattern = value;
            }
        }

        public bool IsLiteral
        {
            get
            {
                return _isLiteral;
            }
            set
            {
                _isLiteral = value;
            }
        }

        public int StateID
        {
            get
            {
                return _iStateID;
            }
            set
            {
                _iStateID = value;
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public bool IsAcceptable
        {
            get
            {
                return _isAcceptable;
            }
            set
            {
                _isAcceptable = value;
            }
        }

        public bool IsCaseSensitive
        {
            get
            {
                return _isCaseSensitive;
            }
            set
            {
                _isCaseSensitive = value;
            }
        }

        public List<GIMachineWordState> NextStates
        {
            get
            {
                return _nextStates;
            }
        }

        public GIMachineWordState Clone()
        {
            GIMachineWordState S = new GIMachineWordState();
            S.IsAcceptable = this.IsAcceptable;
            S.IsCaseSensitive = this.IsCaseSensitive;
            S.IsLiteral = this.IsLiteral;
            S.Name = this.Name;
            S.Pattern = this.Pattern;
            S.StateID = this.StateID;
            
            foreach(GIMachineWordState n in this.NextStates)
            {
                S.NextStates.Add(n.Clone());
            }

            return S;
        }

        public string ToXML()
        {
            StringBuilder sbResult = new StringBuilder();
            System.Xml.XmlWriterSettings oSettings = new System.Xml.XmlWriterSettings();
            oSettings.Encoding = Encoding.ASCII;
            oSettings.Indent = true;
            System.Xml.XmlWriter oWriter = System.Xml.XmlWriter.Create(sbResult, oSettings);

            ToXmlWriter(ref oWriter);


            return sbResult.ToString();
        }

        public void ToXmlWriter(ref System.Xml.XmlWriter oWriter)
        {
            oWriter.WriteStartElement("GIMWordState");

            GIMachine.WriteNodeVale("ID", this.StateID.ToString(), ref oWriter);
            GIMachine.WriteNodeVale("Name", this.Name, ref oWriter);
            GIMachine.WriteNodeVale("Pattern", this.Pattern, ref oWriter);
            GIMachine.WriteNodeVale("IsAcceptable", this.IsAcceptable.ToString().ToLower(), ref oWriter);
            GIMachine.WriteNodeVale("IsLiteral", this.IsLiteral.ToString().ToLower(), ref oWriter);
            GIMachine.WriteNodeVale("IsCaseSensitive", this.IsCaseSensitive.ToString().ToLower(), ref oWriter);

            for (int i = 0; this.NextStates != null && i < this.NextStates.Count; i++)
            {
                this.NextStates[i].ToXmlWriter(ref oWriter);
            }
            oWriter.WriteEndElement();
        }

        public GIMatch Match(string sInput)
        {
            throw new System.NotImplementedException();
        }

        public bool IsAMatch(string sInput)
        {
            throw new System.NotImplementedException();
        }

        public bool IsPrefix(string sInput)
        {
            throw new System.NotImplementedException();
        }
    }
}
